﻿@using NGroup.VTL.Models;
@using System.Collections.Generic;
@using NGroup.VTL.Core.Const;
@using NGroup.VTL.WebCore;
@model List<OrderModel>
@{
    ViewBag.Title = "Quản trị - Danh sách đơn hàng";
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
    PagingInfoModel pagingInfo = ViewBag.PagingInfo as PagingInfoModel;

    string currentFilterProduct = Convert.ToString(ViewBag.currentFilterProduct);
    string currentFilterSupProduct = Convert.ToString(ViewBag.currentFilterSupProduct);
    int index = 0;
}
@section Head{
    <style type="text/css">
        #viewOrder td
        {
            vertical-align: top;
        }

        #viewOrder tr > td:first-child
        {
            padding-top: 5px;
            padding-right: 10px;
            text-align: right;
        }

        #viewOrder textarea
        {
            width: 280px !important;
        }

        #facebox td.body
        {
            width: 400px !important;
        }

        #fbLink
        {
            line-height: 21px;
        }
    </style>
}
<div class="clear">
</div>
<!-- End .clear -->
<div class="content-box">
    @using (Html.BeginForm("quan-ly-don-hang", "vuarao", FormMethod.Post, new { id = "frmPaging" }))
    {
        <!-- Start Content Box -->
        <div class="content-box-header">
            <h3>Danh sách đơn hàng</h3>
            <div class="clear">
            </div>
            <div class="filterGroup">
                <select name="filter_productSupType" id="filter_productSupType" class="inputbox"
                    size="1">
                    @foreach (var item in AppContentTypes.Level0ProductSelectListItems)
                    {
                        if (currentFilterSupProduct == item.Value)
                        {
                        <option value="@item.Value" selected="selected">@item.Text</option>                                    
                        }
                        else
                        {
                        <option value="@item.Value">@item.Text</option>       
                        }
                    }
                </select>
                <select name="filter_productType" id="filter_productType" class="inputbox" size="1">
                    @{
                    List<GenericTypeItem> subItems = new List<GenericTypeItem>();
                    subItems.Add(new GenericTypeItem { Description = "- Nhóm sản phẩm -" });

                    if (!string.IsNullOrWhiteSpace(currentFilterProduct))
                    {
                        subItems.AddRange(AppContentTypes.All.FindAll(e => e.SupTypeID.HasValue && e.TypeID.ToString() == currentFilterProduct));
                    }
                    else
                    {
                        subItems.AddRange(AppContentTypes.All.FindAll(e => e.SupTypeID.HasValue && e.SupTypeID.ToString() == currentFilterSupProduct));
                    }

                    foreach (var item in subItems)
                    {
                        if (item.TypeID.ToString() == currentFilterProduct)
                        {
                        <option value="@item.TypeID" selected="selected">@item.Description</option>
                        }
                        else
                        {
                        <option value="@item.TypeID">@item.Description</option>
                        }
                    }
                    }
                </select>
                <input type="submit" class="button" value="Lọc" />
            </div>
            <div class="clear">
            </div>
        </div>
        <div class="clear">
        </div>
        <!-- End .content-box-header -->
        <div class="content-box-content">
            <div class="tab-content default-tab" id="tab1">
                @if (Model != null && Model.Count > 0)
                {
                    <table>
                        <thead>
                            <tr>
                                <th>#
                                </th>
                                <th>Người đặt hàng
                                </th>
                                <th>Ngày đặt hàng
                                </th>
                                <th>Loại sản phẩm
                                </th>
                                <th>Số lượng
                                </th>
                                <th>Thành tiền
                                </th>
                                <th>&nbsp;</th>
                            </tr>
                        </thead>
                        <tfoot>
                            <tr>
                                <td colspan="7">
                                    @Html.Partial("PagingPartial", pagingInfo)
                                </td>
                            </tr>
                        </tfoot>
                        <tbody>
                            @foreach (OrderModel item in Model)
                            {
                                index++;
                                
                                <tr>
                                    <td>
                                        @index
                                        <input class="orderedUrl" type="hidden" value="@(item.SupTypeIDString != null && item.TypeIDString != null && item.UniqueString != null ? string.Format("/{0}/{1}/{2}", item.SupTypeIDString, item.TypeIDString, item.UniqueString) : string.Empty)" />
                                        <input class="customerPhone" type="hidden" value="@item.CustomerPhone" />
                                        <input class="shippingAddress" type="hidden" value="@item.ShippingAddress" />
                                    </td>
                                    <td>
                                        <span class="customerName">@item.CustomerName</span>
                                    </td>
                                    <td>
                                        @{
                                var createdAt = item.OrderDate.ToString("dd-MM-yyyy");

                                if (!string.IsNullOrWhiteSpace(createdAt))
                                {
                                            <span class="createdAt">@createdAt</span>
                                }
                                else
                                {
                                            <span class="createdAt">-</span>
                                }
                                        }
                                    </td>
                                    <td>
                                        <span class="productType">@AppContentTypes.All.Find(e => !e.SupTypeID.HasValue && e.TypeID == item.ProductSupTypeID).Description
                                            -
                                        @AppContentTypes.All.Find(e => e.SupTypeID.HasValue && e.TypeID == item.ProductTypeID).Description</span>
                                    </td>
                                    <td>
                                        <span class="orderAmount">@item.Amount</span>
                                    </td>
                                    <td>
                                        @if (item.TotalPrice == 0 || string.IsNullOrEmpty(item.TotalPrice.ToString()))
                                        {
                                            <span class="totalPrice">N/A</span>
                                        }
                                        else
                                        {
                                            <span class="totalPrice">@item.TotalPrice.ToString("#,##0 VNĐ")</span>
                                        }
                                    </td>
                                    <td>
                                        <!-- Icons -->
                                        <a href="#formViewOrder" title="View" rel="modal" class="btnViewOrder">
                                            <img src="@Url.Content("~/Content/adminpages/images/icons/eye.png")" alt="View" /></a>
                                        <a href="/vuarao/delete-order/@item.ID" class="btnDelete" title="Delete">
                                            <img src="@Url.Content("~/Content/adminpages/images/icons/cross.png")" alt="Delete" /></a>
                                    </td>
                                </tr>
                            }
                        </tbody>
                    </table>
                }
                else
                {
                    <div style="padding: 20px;">
                        <p>
                            Hiện tại chưa có đơn hàng nào!
                        </p>
                    </div>
                }
            </div>
        </div>
        <!-- End .content-box-content -->
    }
    <div class="nodisplay">

        <form id="formViewOrder" method="post">
            <table id="viewOrder">
                <tr>
                    <td style="padding-top: 5px; padding-bottom: 10px;">Sản phẩm</td>
                    <td>
                        <a href="" id="fbLink" target="_blank">Link chi tiết</a>
                    </td>
                </tr>
                <tr>
                    <td>Số lượng</td>
                    <td>
                        <input class="text-input medium-input" id="fbAmount" type="text" />
                    </td>
                </tr>
                <tr>
                    <td>Điện thoại</td>
                    <td>
                        <input class="text-input medium-input" id="fbCustomerPhone" type="text" /></td>
                </tr>
                <tr>
                    <td>Họ tên</td>
                    <td>
                        <input class="text-input medium-input" id="fbCustomerName" type="text" /></td>
                </tr>
                <tr>
                    <td>Địa chỉ giao hàng</td>
                    <td>
                        <textarea cols="15" rows="2" id="fbShippingAddress"></textarea></td>
                </tr>
                <tr>
                    <td style="padding-top: 15px; padding-bottom: 15px;">Thành tiền</td>
                    <td style="padding-top: 15px; padding-bottom: 15px;"><span style="font-weight: bold;" id="fbTotalPrice">-</span></td>
                </tr>
            </table>
        </form>
    </div>
</div>
@section Lazy{
    <script type="text/javascript">
        $(".current").removeClass("current");        
        $("#qldh").addClass("current");
        var __productsTypes = @Html.Raw(AppContentTypes.ProductSelectListItemsJson);

        $(document).ready(function(){
            $("#filter_productSupType").change(function () {
                UpdateProductFilterList();
            });

            $(".btnViewOrder").click(function(){
                var currentRow = $(this).parents("tr").get(0);

                if(!currentRow == false)
                {
                    var link = $($(currentRow).find("td .orderedUrl").get(0)).val();
                    var customerName = $($(currentRow).find("td .customerName").get(0)).text();
                    var createdAt = $($(currentRow).find("td .createdAt").get(0)).text();
                    var productType = $($(currentRow).find("td .productType").get(0)).text();
                    var orderAmount = $($(currentRow).find("td .orderAmount").get(0)).text();
                    var totalPrice = $($(currentRow).find("td .totalPrice").get(0)).text();
                    var customerPhone = $($(currentRow).find("td .customerPhone").get(0)).val();
                    var shippingAddress = $($(currentRow).find("td .shippingAddress").get(0)).val();

                    if(!link == false)
                    {
                        $("#facebox #fbLink").attr("href", link);
                    }
                    else
                    {
                        $("#facebox #fbLink").attr("href", "#");
                        $("#facebox #fbLink").text("Sản phẩm đã bị xóa!");
                    }
                    
                    $("#facebox #fbAmount").val(orderAmount);
                    $("#facebox #fbCustomerName").val(customerName);
                    $("#facebox #fbCustomerPhone").val(customerPhone);
                    $("#facebox #fbShippingAddress").val(shippingAddress);
                    $("#facebox #fbTotalPrice").text(totalPrice);
                }                
            });

        });

        function UpdateProductFilterList()
        {
            var selectedID = $("#filter_productSupType option:selected").val();

            var subItems = __productsTypes[selectedID];
            $("#filter_productType").empty();
            $("#filter_productType").append("<option value=''>- Nhóm sản phẩm -</option>");

            if (!subItems == false && subItems.length > 0) {
                for (var i = 0; i < subItems.length; i++) {
                    var option = "<option value='" + subItems[i].Value + "'>" + subItems[i].Text + "</option>";
                    $("#filter_productType").append(option);      
                }           
            }
        }

    </script>
}
